package com.miracle9.game.dao;

import com.miracle9.game.entity.DragonTigerDesk;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DragonTigerDeskDao extends SqlOperating<DragonTigerDesk, Integer> {

    public List<Integer> getDeskIdsByRoomId(int roomId) {
        return (List<Integer>) getSession().createQuery("select id from DragonTigerDesk where roomId = ?").setParameter(0, roomId).list();
    }

    @Cacheable(value = {"springCache"}, key = "'getAllDragonTigerDesk'")
    public List<DragonTigerDesk> getAllDesk() {
        return q_CreateSqlList("from DragonTigerDesk order by roomId desc,orderBy", new Object[0]);
    }

    @Cacheable(value = {"springCache"}, key = "'getDragonTigerDesk'+#id")
    public DragonTigerDesk getDesk(int id) {
        return (DragonTigerDesk) getById(Integer.valueOf(id));
    }

    @Cacheable(value = {"springCache"}, key = "'countDragonTigerDesk'+#roomId")
    public int getQuantityByRoomId(int roomId) {
        return q_CreateSqlInt("select count(*) from DragonTigerDesk where roomId=?", new Object[]{Integer.valueOf(roomId)});
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getGoldSharkAllDesk'"), @CacheEvict(value = {"springCache"}, key = "'countDragonTigerDesk'+#dragonTigerDesk.roomId")})
    public Integer saveReturnInteger(DragonTigerDesk dragonTigerDesk) {
        return (Integer) saveReturn(dragonTigerDesk);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getDragonTigerDesk'+#dragonTigerDesk.id"), @CacheEvict(value = {"springCache"}, key = "'getAllDragonTigerDesk'")})
    public void updateDeskInfo(DragonTigerDesk dragonTigerDesk) {
        DragonTigerDesk desk = getDesk(dragonTigerDesk.getId());
        desk.setRoomId(dragonTigerDesk.getRoomId());
        desk.setName(dragonTigerDesk.getName());
        desk.setMinGold(dragonTigerDesk.getMinGold());
        desk.setMinBet(dragonTigerDesk.getMinBet());
        desk.setMaxBet(dragonTigerDesk.getMaxBet());
        desk.setTieMaxBet(dragonTigerDesk.getTieMaxBet());
        desk.setBetTime(dragonTigerDesk.getBetTime());
        desk.setExchange(dragonTigerDesk.getExchange());
        desk.setOnceExchangeValue(dragonTigerDesk.getOnceExchangeValue());
        desk.setSiteType(dragonTigerDesk.getSiteType());
        desk.setWaterType(dragonTigerDesk.getWaterType());
        desk.setWaterValue(dragonTigerDesk.getWaterValue());
        desk.setAutoKick(dragonTigerDesk.getAutoKick());
        desk.setState(dragonTigerDesk.getState());
        desk.setBetChip(dragonTigerDesk.getBetChip());
        update(desk);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getDragonTigerDesk'+#id"), @CacheEvict(value = {"springCache"}, key = "'getAllDragonTigerDesk'"), @CacheEvict(value = {"springCache"}, key = "'countDesk'+#roomId")})
    public void deleteById(int id, int roomId) {
        deleteById(Integer.valueOf(id));
    }

    @CacheEvict(value = {"springCache"}, key = "'getDragonTigerDesk'+#id")
    public void initializeById(int id) {
        q_CreateSql("update DragonTigerDesk set sumYaFen = 0,sumDeFen=0 where id=?", new Object[]{Integer.valueOf(id)}).executeUpdate();
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getAllDragonTigerDesk'")})
    public void updateOrderByById(int id, int index) {
        q_CreateSql("update DragonTigerDesk set orderBy = ? where id=?", new Object[]{Integer.valueOf(index), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateStateById(int id, int state) {
        q_CreateSql("update DragonTigerDesk set state=? where id=?", new Object[]{Integer.valueOf(state), Integer.valueOf(id)}).executeUpdate();
    }

}
